package org.dromara.system.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.domain.protocol.SensorData;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class DeviceUtils {

    private final RestTemplate restTemplate;

    public List<SensorData> getDeviceList() {
       String url="http://10.122.41.8:8080/ShmBridgeCheck/s/screen/getRealtimeDataBatch";
        String body = sendGetHttpRequest(url, null);
        FireRs<List<SensorData>> rs = JSON.parseObject(body, new TypeReference<>() {});
        log.info("Device list fetched: {}", rs.getResults());
        return rs.getResults();

    }


    private String sendGetHttpRequest(String url, String requestBody) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
        try {
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
            return response.getBody();
        } catch (Exception e) {
            log.error("请求失败: URL={}, 请求体={}, 错误信息={}", url, requestBody, e.getMessage());
            return ""; // 返回空字符串或默认值
        }
    }
}
